package com.lemon.smartsafety.ui.adapter.safe.safe_manager;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.android.http.HttpUtils;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.safe.safe_manager.CompanySafeManageHistoryItemVo;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafety.utils.photo.GlideImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.finalteam.galleryfinal.model.PhotoInfo;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * 项目名称：workspace
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/6/7 14:58
 * 修改人：Fargo
 * 修改时间：2016/6/7 14:58
 * 修改备注：
 */
public class SafeManagerHistoryItemAdapter extends LemonRcvAdapter<CompanySafeManageHistoryItemVo> {

    private Activity mActivity;

    private RecyclerTClickListener<CompanySafeManageHistoryItemVo> mListener;

    public SafeManagerHistoryItemAdapter(@Nullable List<CompanySafeManageHistoryItemVo> data) {
        super(data);
    }

    public SafeManagerHistoryItemAdapter(Activity activity, List<CompanySafeManageHistoryItemVo> data, RecyclerTClickListener<CompanySafeManageHistoryItemVo> listener) {
        super(data);
        this.mListener = listener;
        this.mActivity = activity;
    }

    public int getPhotoSize(String path1,String path2,String path3){
        int item=0;
        if(!StringUtils.isEmpty(path1))
        {
            item=item+1;
        }

        if(!StringUtils.isEmpty(path2))
        {
            item=item+1;
        }

        if(!StringUtils.isEmpty(path3))
        {
            item=item+1;
        }

        return item;
    }

    public List<PhotoInfo>  getPhotoList (String path1,String path2,String path3){
        List<PhotoInfo> list=new ArrayList<>();
        if(!StringUtils.isEmpty(path1))
        {
            PhotoInfo mPhotoInfo=new PhotoInfo();
            mPhotoInfo.setPhotoPath( HttpUtils.formatFilesUrl(path1));
            list.add(mPhotoInfo);
        }

        if(!StringUtils.isEmpty(path2))
        {
            PhotoInfo mPhotoInfo=new PhotoInfo();
            mPhotoInfo.setPhotoPath( HttpUtils.formatFilesUrl(path2));
            list.add(mPhotoInfo);
        }

        if(!StringUtils.isEmpty(path3))
        {
            PhotoInfo mPhotoInfo=new PhotoInfo();
            mPhotoInfo.setPhotoPath( HttpUtils.formatFilesUrl(path3));
            list.add(mPhotoInfo);
        }

        return list;
    }






    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new AdapterItemView();
    }

    public class AdapterItemView implements AdapterItem<CompanySafeManageHistoryItemVo> {


        @Bind(R.id.imageArrow)
        ImageView imageArrow;
        @Bind(R.id.txt_item_name)
        TextView txtItemName;
        @Bind(R.id.txt_last_check_user)
        TextView txtLastCheckUser;
        @Bind(R.id.txt_last_check_time)
        TextView txtLastCheckTime;
        @Bind(R.id.txt_check_result)
        TextView txtCheckResult;
        @Bind(R.id.image_picture1)
        ImageView imagePicture1;
        @Bind(R.id.image_picture2)
        ImageView imagePicture2;
        @Bind(R.id.image_picture3)
        ImageView imagePicture3;
        @Bind(R.id.txt_check_result_title)
        TextView txtCheckResultTitle;
        @Bind(R.id.line_img)
        LinearLayout lineImg;
        private CompanySafeManageHistoryItemVo mData;
        private int mPosition;
        private PhotoInfo mPhotoInfo;

        private List<PhotoInfo> mList;


        @Override
        public int getLayoutResId() {
            return R.layout.item_safe_manager_history_item;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
        /*    mainCardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(mData, mPosition);

                }
            });*/
//            tvGoDetail.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    System.out.println("detail");
//
//                }
//            });

            imageArrow.setVisibility(View.GONE);

            imagePicture1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!StringUtils.isEmpty(mData.getPicture1())&& !HttpUtils.isIPX(mData.getPicture1())) {
                        if(getPhotoSize(mData.getPicture1(),mData.getPicture2(),mData.getPicture3())!=1){
                            UIHelper.showPhotoView(mActivity, getPhotoList(mData.getPicture1(),mData.getPicture2(),mData.getPicture3()),0);
                        }else {
                            mPhotoInfo = new PhotoInfo();
                            mPhotoInfo.setPhotoPath(HttpUtils.formatFilesUrl(mData.getPicture1()));
                            UIHelper.showPhotoView(mActivity, mPhotoInfo);
                        }
                    }
                }
            });

            imagePicture2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (!StringUtils.isEmpty(mData.getPicture2())&& !HttpUtils.isIPX(mData.getPicture2())) {
                        if(getPhotoSize(mData.getPicture1(),mData.getPicture2(),mData.getPicture3())!=1){
                            UIHelper.showPhotoView(mActivity, getPhotoList(mData.getPicture1(),mData.getPicture2(),mData.getPicture3()),1);
                        }else {
                            mPhotoInfo = new PhotoInfo();
                            mPhotoInfo.setPhotoPath(HttpUtils.formatFilesUrl(mData.getPicture2()));
                            UIHelper.showPhotoView(mActivity, mPhotoInfo);
                        }
                    }
                }
            });

            imagePicture3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (!StringUtils.isEmpty(mData.getPicture3())&& !HttpUtils.isIPX(mData.getPicture3())) {
                        if(getPhotoSize(mData.getPicture1(),mData.getPicture2(),mData.getPicture3())!=1){
                            UIHelper.showPhotoView(mActivity, getPhotoList(mData.getPicture1(),mData.getPicture2(),mData.getPicture3()),2);
                        }else {
                            mPhotoInfo = new PhotoInfo();
                            mPhotoInfo.setPhotoPath(HttpUtils.formatFilesUrl(mData.getPicture3()));
                            UIHelper.showPhotoView(mActivity, mPhotoInfo);
                        }
                    }
                }
            });

        }

        @Override
        public void handleData(CompanySafeManageHistoryItemVo data, int position) {
            mData = data;
            mPosition = position;

            txtItemName.setText(StringUtils.nullValue(data.getItem_name()));
            txtLastCheckUser.setText(StringUtils.nullValue(data.getLast_check_user()));
            txtLastCheckTime.setText(StringUtils.nullValue(data.getLast_check_time()));
            if (!StringUtils.isEmpty(data.getCheck_result())) {
                String checkResult = StringUtils.nullValue(data.getCheck_result());
                if (checkResult.length() != 0 && checkResult.indexOf(",") != -1) {

                    String strCheckResult=checkResult.substring(data.getCheck_result().indexOf(",")+1,checkResult.length());
                    switch (data.getCheck_result().substring(0, data.getCheck_result().indexOf(","))) {
                        case "1":
                            txtCheckResult.setText(StringUtils.nullValue(data.getCheck_result()));
                            txtCheckResult.setText(strCheckResult);
                            txtCheckResult.setTextColor(IApplication.context().getResources().getColor(R.color.light_green));
                            txtCheckResultTitle.setTextColor(IApplication.context().getResources().getColor(R.color.light_green));
                            break;
                        case "0":
                            txtCheckResult.setText(StringUtils.nullValue(data.getCheck_result()));
                            txtCheckResult.setText(strCheckResult);
                            txtCheckResult.setTextColor(IApplication.context().getResources().getColor(R.color.red));
                            txtCheckResultTitle.setTextColor(IApplication.context().getResources().getColor(R.color.red));
                            break;
                        default:
                            txtCheckResult.setText("");
                            break;
                    }

                }
            }

            if (StringUtils.isEmpty(data.getPicture1()) && StringUtils.isEmpty(data.getPicture2()) && StringUtils.isEmpty(data.getPicture3())) {

                lineImg.setVisibility(View.GONE);
            } else {

                lineImg.setVisibility(View.VISIBLE);
                if (!StringUtils.isEmpty(data.getPicture1())&& !HttpUtils.isIPX(data.getPicture1())) {
                    imagePicture1.setTag(StringUtils.nullValue(HttpUtils.formatFilesUrl(data.getPicture1())));
                    GlideImageLoader.loadImage(mActivity, HttpUtils.formatFilesUrl(data.getPicture1()), imagePicture1);
                    //LoggerHelper.error(SafeManagerHistoryItemAdapter.class,HttpUtils.formatFilesUrl(data.getPicture1()));
                }
                if (!StringUtils.isEmpty(data.getPicture2())&& !HttpUtils.isIPX(data.getPicture2())) {
                    imagePicture2.setTag(StringUtils.nullValue(HttpUtils.formatFilesUrl(data.getPicture2())));
                    GlideImageLoader.loadImage(mActivity, HttpUtils.formatFilesUrl(data.getPicture2()), imagePicture2);
                }
                if (!StringUtils.isEmpty(data.getPicture3())&& !HttpUtils.isIPX(data.getPicture3())) {
                    imagePicture3.setTag(StringUtils.nullValue(HttpUtils.formatFilesUrl(data.getPicture3())));
                    GlideImageLoader.loadImage(mActivity, HttpUtils.formatFilesUrl(data.getPicture3()), imagePicture3);
                }


            }


        }


    }
}
